//
//  TestTableViewCell.m
//  10-UITableViewCell
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TestTableViewCell.h"
#import "UIView+ChangeFrame.h"
// 使用@class声明的类 需要在.m中去import
#import "NewsObject.h"

@interface TestTableViewCell ()
@property (nonatomic, strong) UIImageView *imageViewPho;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UILabel *labelDetail;
@property (nonatomic, strong) UILabel *labelFllow;
@end

@implementation TestTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _imageViewPho = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_imageViewPho];
        
        _labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        _labelTitle.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_labelTitle];
        
        _labelDetail = [[UILabel alloc] initWithFrame:CGRectZero];
        _labelDetail.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_labelDetail];
        
        _labelFllow = [[UILabel alloc] initWithFrame:CGRectZero];
        _labelFllow.backgroundColor = [UIColor cyanColor];
        // label上不要加视图 !!!!~~~
        [_labelDetail addSubview:_labelFllow];
    }
    return self;
}
// 有内容/坐标改变时, 会触发该方法
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftMargin = 5;
    CGFloat topMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat middleMargin = 5;
    _imageViewPho.frame = CGRectMake(leftMargin, topMargin, self.contentView.z_width * 0.3, self.contentView.z_height - topMargin - bottomMargin);
    CGFloat titleX = _imageViewPho.z_x + _imageViewPho.z_width + middleMargin;
    _labelTitle.frame = CGRectMake(titleX, topMargin, self.contentView.z_width - titleX - rightMargin, 20);
    
    CGFloat labelDetailY = topMargin + _labelTitle.frame.size.height + middleMargin;
    _labelDetail.frame = CGRectMake(titleX, labelDetailY, _labelTitle.frame.size.width, self.contentView.z_height - labelDetailY - bottomMargin);
    
    CGFloat fllowX = _labelDetail.z_width * 0.6;
    CGFloat fllowWidth = _labelDetail.z_width * 0.4;
    CGFloat fllowHeight = _labelDetail.z_height / 2;
    // 判断父视图是不是_labelDetail
    if (_labelFllow.superview == _labelDetail) {
        _labelFllow.frame = CGRectMake(fllowX, fllowHeight, fllowWidth, fllowHeight);
        CGRect newRect = [_labelFllow.superview convertRect:_labelFllow.frame toView:self.contentView];
        _labelFllow.frame = newRect;
        // labelFllow转换坐标后添加到contentView上
        [self.contentView addSubview:_labelFllow];
    }
}

- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _imageViewPho.image = [UIImage imageNamed:newsObject.newsImage];
    _labelTitle.text = newsObject.title;
    _labelDetail.text = newsObject.newsDescription;
    _labelFllow.text = newsObject.newsNumberCount;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
